﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCM.UI.Utils;

namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCM90 : EFViewCmes
    {

        private MonthBlock monthBlock = new MonthBlock();//构造前台是否下拉列表

        public ZMCM90()
        {
            InitializeComponent();
            this.BeforeAction += OnBeforeAction;
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
        }


        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();

            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击设置安全库存按钮，弹出编辑页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnSafebasenumButtonClick(object sender, RoutedEventArgs e)
        {

            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "库存信息-设置安全库存", "update", null, sender, e);
        }


        /// <summary>
        /// 点击结转按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnJiezhuanButtonClick(object sender, RoutedEventArgs e)
        {

            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            EIInfo eiInfo = new EIInfo();

            base.OnInsertButtonClickPopup(eiInfo, "库存信息-库存结转", "jiezhuan", null, sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo eIInfo, string title, string dialogType, string _args)
        {
            if (dialogType.Equals("update"))
            {

                MaterialBase materialBase = new MaterialBase(dialogType);//构造一个新的修改页面
                materialBase.EIDataContext = new Platform.Core.UI.EIDataContext();
                eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));
                materialBase.EIDataContext.Update(eIInfo);//数据渲染
                CmesWindows _popup = new CmesWindows();//构造一个弹出页面
                _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _popup.Header = title == null ? "" : title;//设置Title
                _popup.Content = materialBase;//让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
                _popup.ShowDialog();//模态显示弹出窗口

            }
            if (dialogType.Equals("jiezhuan"))
            {

                MaterialBaseHis materialBaseHis = new MaterialBaseHis(dialogType);//构造一个新的修改页面
                materialBaseHis.EIDataContext = new Platform.Core.UI.EIDataContext();
                eIInfo.AddBlock(monthBlock.GetMonth2());//月份下拉框
                materialBaseHis.EIDataContext.Update(eIInfo);//数据渲染

                materialBaseHis.fdHisDate.Value = System.DateTime.Now.Date;
                materialBaseHis.fdYear.Value = System.DateTime.Now.Year;

                string Month = "01";
                if (System.DateTime.Now.Month < 10)
                {
                    Month = "0" + System.DateTime.Now.Month;
                }
                else {
                    Month = System.DateTime.Now.Month.ToString();
                }
                materialBaseHis.fdMonth.Value = Month;

                CmesWindows _popup = new CmesWindows();//构造一个弹出页面
                _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _popup.Header = title == null ? "" : title;//设置Title
                _popup.Content = materialBaseHis;//让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
                _popup.ShowDialog();//模态显示弹出窗口

            }

        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }
            //按钮生效
            base.TempButtonOpen(true);
            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }



        /// <summary>
        /// 页签改变加载不同的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EFTab_SelectionChanged(object sender, RoutedEventArgs e)
        {
            EFTab tab = sender as EFTab;
            if (tab.SelectedItem != null)
            {
                if (tab.SelectedItem.Equals(this.tab2))
                {
                    Dictionary<string, string> args2 = new Dictionary<string, string>();
                    //args2.Add("problemViewType", "Submit");
                    this.tab2.LoadView("ZMCM91", args2);
                }
            }
        }

    }
}
